﻿//导入表定义
document.write("<script language='javascript' src='/resources/js/views/defineTable.js'></script>");
document.write("<script language='javascript' src='/resources/js/component.js'></script>");
var action_indicator_count_monitor="/indicator/count/monitor.do";
var allYear="/resources/js/views/allYear.do";
var action_indicator_type_all="/indicator/type/all.do" //--------------领域

var color_memo_html =
	'\t\t\t\t<div style="width: auto; padding:  5px; ">\n' +
	'\t\t\t\t\t<table style="border-collapse: separate; ">\n' +
	'\t\t\t\t\t\t<tr>\n' +
	'\t\t\t\t\t\t\t<td style="width: 30px; background-color: limegreen; padding-right:  10px; border-radius: 5px;"></td>\n' +
	'\t\t\t\t\t\t\t<td style="padding-right: 10px; border-color: #000000; ">提前达标</td>\n' +
	'\t\t\t\t\t\t\t<td style="width: 30px; background-color: blue; padding-right : 10px; border-radius: 5px;"></td>\n' +
	'\t\t\t\t\t\t\t<td style="padding-right: 10px; border-color: black;">达到当年标准</td>\n' +
	'\t\t\t\t\t\t\t<td style="width: 30px; background-color: orange; padding-right : 10px; border-radius: 5px;"></td>\n' +
	'\t\t\t\t\t\t\t<td style="padding-right: 10px; border-color: black;">当年未达标准预期可达</td>\n' +
	'\t\t\t\t\t\t\t<td style="width: 30px; background-color: coral; padding-right : 10px; border-radius: 5px;"></td>\n' +
	'\t\t\t\t\t\t\t<td style="padding-right: 10px; border-color: black;">达标困难</td>\n' +
	'\t\t\t\t\t\t\t<td style="width: 30px; background-color: red; padding-right : 10px; border-radius: 5px;"></td>\n' +
	'\t\t\t\t\t\t\t<td style="padding-right: 10px; border-color: black;">预期难以达标</td>\n' +
	'\t\t\t\t\t\t</tr>\n' +
	'\t\t\t\t\t</table>\n' +
	'\t\t\t\t</div>';

var page = 1;
var pageSize = 200;


function loadYear(){
	$.get(allYear, function (json) {
		$("#comboboxYear").combobox({
			textField:"text",
			valueField:"id",
			width:'100px',
			height:'28px',
			data:json,
			multiple:true,
			editable:false,
			formatter:function(row){
				var opts;
				if(row.select=="true"){
					row.selected=true;
					opts = "<input type='checkbox' name='year' checked='checked' id='"+row.id+"' value='"+row.id+"'>"+row.text+"</input>";
				}else{
					opts = "<input type='checkbox' name='year' id='"+row.id+"' value='"+row.id+"'>"+row.text+"</input>";
				}
				return opts;
			},
			panelHeight:"auto",
			panelWidth:'100px',
			panelAlign:'left',
			onSelect:function(rec) {
				oCheckbox = document.getElementById(rec.id);
				oCheckbox.checked=true;
				query(page,pageSize);
			},
			onUnselect:function(rec){
				oCheckbox = document.getElementById(rec.id);
				oCheckbox.checked=false;
				query(page,pageSize);
			}
			// ,
			// onLoadSuccess:function(){
			// 	queryList();
			// }
		});
	},'json');
}

function loadIndicatorTypeAll(){
	var formsid= $('#formid').combobox("getValue");
	$.getJSON(action_indicator_type_all,{'formsid':formsid}, function (json) {
		if(!json.success){
			$.messager.alert('提示',json.massage);
			return;
		}
		json.data.unshift({
			id : -1,
			name : "全部"
		});
		$('#indicator_type_select').combobox({
			async : false,
			method : 'GET',
			data : json.data,
			valueField : 'id',
			textField : 'name',
			fitColumns:false,
			collapsible: true,
			singleSelect : false,// 是否单选
			checkOnSelect: true,//选中行后,checkbox选中
			selectOnCheck: true,//选中行后,checkbox选中
			multiple:true,
			editable:false,
			formatter:function(row){
				var opts;
				if(row.selected==true){
					opts = "<input type='checkbox' checked='checked' id='"+row.id+"' value='"+row.id+"'>"+row.name+"</input>";
				}else{
					opts = "<input type='checkbox' id='"+row.id+"' value='"+row.id+"'>"+row.name+"</input>";
				}
				return opts;
			},
			// 默认加载领域数据
			onLoadSuccess : function() {
				var a = $('#indicator_type_select').combobox('getData');
				if (a.length > 0) {
					$("#indicator_type_select").combobox('select', a[0].id);
				}
			}
			// ,
			// onSelect:function () {
			// 	query(page,pageSize);
			// }
		})
	},'json');
}

// ----------------画图------------------------------------
function showData(series, jsonXData) {
	var field = $('#indicator_type_select').combobox("getText");    //领域编号
	$("#view_containerzhuzi").height(iHeight/2);
	$("#view_containerzhuzi").width($("#tabDiv").width()-10);
	var myChart = echarts.init(document.getElementById('view_containerzhuzi'));
// 指定图表的配置项和数据
	option = {
		title: {
			text: '省级-' + field + '领域监测指标达标情况分析'
		},
		legend: {
			x: 'center',
			padding:30,
			y: 'top',
			data: ['提前达标', '达到当年标准', '当年未达标经努力预期可达标', '达标困难', '预期难以达标']
		},
		tooltip: {
			trigger: 'axis'
		},
		toolbox: {
			show: true,
			orient: 'vertical',
			y: 'center',
			itemSize: 24,
			padding: 20,
			feature: {
				mark: {show: true},
				myTool: {
					show: true,
					title: '横向',
					icon: 'image:///resources/images/hh.png',
					onclick: function () {
						myChart.setOption({
							xAxis: [
								{
									type: 'value'
								}
							],
							yAxis: [
								{
									type: 'category',
									data: jsonXData
								}
							]
						});
					}
				}, myTool1: {
					show: true,
					title: '竖向',
					icon: "M6.7,22.9h10V48h-10V22.9zM24.9,13h10v35h-10V13zM43.2,2h10v46h-10V2zM3.1,58h53.7",
					onclick: function () {
						myChart.setOption({
							xAxis: [
								{
									type: 'category',
									data: jsonXData
								}
							],
							yAxis: [
								{
									type: 'value'
								}
							]
						});
					}
				},
				magicType: {show: true, type: ['stack', 'tiled', 'pie', 'funnel']},
				saveAsImage: {
					show: true,
					title: '保存为图片',
					type: 'png',
					lang: ['点击保存']
				},
				restore: {show: true}
			}
		},
		calculable: true,
		xAxis: [
			{
				type: 'category',
				"axisLabel": {
					"show": true,
					"splitNumber": 15,
					"textStyle": {
						"fontFamily": "微软雅黑",
						"fontSize": 12
					}
				},
				data: jsonXData
			}
		],
		yAxis: [
			{
				type: 'value'
			}
		],
		series: series,
		label: {
			normal: {
				show: true,
				position: 'top',
				textStyle: {"fontFamily": "微软雅黑", "font-weight": "bold", color: '#515151'}
			}
		},
		color: ['#009966', '#039dea', '#ffff00', '#FF9933', '#ff0000']
	};
// 使用刚指定的配置项和数据显示图表。
	myChart.setOption(option);
	$('#view_dark').click(function () {
		myChart = echarts.init(document.getElementById('view_containerzhuzi'),'dark');
		myChart.setOption(option);
	});
	$('#view_infographic').click(function () {
		myChart = echarts.init(document.getElementById('view_containerzhuzi'),'infographic');
		myChart.setOption(option);
	});
	$('#view_macarons').click(function () {
		myChart = echarts.init(document.getElementById('view_containerzhuzi'),'macarons');
		myChart.setOption(option);
	});
	$('#view_vintage').click(function () {
		myChart = echarts.init(document.getElementById('view_containerzhuzi'),'vintage');
		myChart.setOption(option);
	});
	$('#view_default').click(function () {
		myChart = echarts.init(document.getElementById('view_containerzhuzi'));
		myChart.setOption(option);
	});
	myChart.setOption(option);
	$('#view_landscape').click(function () {
		myChart.setOption({
			xAxis: [
				{
					type: 'value'
				}
			],
			yAxis: [
				{
					type: 'category',
					data: jsonXData
				}
			]
		})
	});
	myChart.setOption(option);
	$('#view_reset').click(function () {
		myChart.setOption({
			xAxis: [
				{
					type: 'category',
					data: jsonXData
				}
			],
			yAxis: [
				{
					type: 'value'
				}
			]
		})
	});

	$('#view_change').show();
	$('#view_colorSelect').empty();
}

var dataLength=0;
//预警指标占比分析出图
function showImage(index, pageType){

	var yeares = document.getElementsByName("year");
	var yearList = [];
	for (var i = 0; i < yeares.length; i++) {
		yearList.push(yeares[i].value);
	}
	dataLength=yearList.length;
	var year=yearList[index];

	var para_type="";
	var title_type="";
	if(pageType=="PAGETYPE_PROVINCE"){
		para_type= {"year":year,"formsid":1};
		title_type= {text: year+'年省级各领域监测指标占比分析（%）',};
	}else if(pageType=="PAGETYPE_CITY"){
		var areaid= $('#city_departmentSelect').combobox("getValue");
		para_type={"years":year, "areaid": areaid,"formsid":1};
		var cityName= $('#city_departmentSelect').combobox("getText");
		title_type={text: year+'年'+cityName+'各领域监测指标占比分析（%）',};
	}else if(pageType=="PAGETYPE_COUNTY"){
		var areaid= $('#countySelect').combobox("getValue");
		para_type={"years":year, "areaid": areaid,"formsid":1};
		var cityName= $('#countySelect').combobox("getText");
		title_type= {text: year+'年'+cityName+'各领域监测指标占比分析（%）',};
	}else{
		return;
	}

	$.get(action_indicator_count_monitor, para_type, function (json) {
		if(json.code != 200){
			return;
		}
		var data=json.data;
		var total=data.size;
		data=data["results"];
		if(total==null||total<1){
			return;
		}
		var yAxisDatas=[];
		var seriesDatas=[];
		for (var i = 0; i < data.length; i++) {
			yAxisDatas.push(data[i].name);
			seriesDatas.push(parseFloat(((data[i].count/total)*100).toFixed(2)));
		}

		$("#view_containerzhuzi2").height(iHeight/2);
		$("#view_containerzhuzi2").width($("#tabDiv").width()-10);
		var myChart = echarts.init(document.getElementById('view_containerzhuzi2'));
		// 指定图表的配置项和数据
		var	option2  = {
			color:['#71e9f4'],
			title : title_type,
			tooltip : {trigger: 'axis'},
			toolbox: {
				show : true,
				feature : {
					mark : {show: true},
					saveAsImage : {show: true}
				}
			},
			calculable : true,
			xAxis :[{
				type : 'category',
				data : yAxisDatas
			}],
			yAxis :[{
				type : 'value',
				boundaryGap : [0, 0.01]
			}],
			series : [{
				name:'占比',
				type:'bar',
				label: {
					normal: {
						formatter : function (params){return (JSON.stringify(params.value)) + '%';},
						position: 'top',
						show: true
					}
				},
				data:seriesDatas
			}]
		};
		myChart.setOption(option2,true);
		var zr = myChart.getZr();
		var icon0 = new echarts.graphic.Circle({
			shape: { r: 20 },
			style: {
				text: '<',
				fill: '#eee'
			},
			position: [20, zr.getHeight() / 2]
		});

		var icon1 = new echarts.graphic.Circle({
			shape: { r: 20 },
			style: {
				text: '>',
				fill: '#eee'
			},
			position: [zr.getWidth() - 20, zr.getHeight() / 2]
		});

		var group = new echarts.graphic.Group();
		group.add(icon0);
		group.add(icon1);
		zr.add(group);
		icon0.on('click', function () {
			index -= 1;
			if (index < 0) {
				index += dataLength;
			}
			showImage(index)
		});
		icon1.on('click', function () {
			index = (index + 1) % dataLength;
			showImage(index);
		});

	},"JSON");
}

var jsonXData = [];    //年份数组 放在图里
var series = [];
// -----新加统计颜色数量:不要分页，加载所有
function checkChangeImage(data, yearList) {
	var color_Data = [];   // 颜色分布数组
	for (var j = 0; j < yearList.length; j++) {
		var years_Data = {};
		var red = 0;
		var orange = 0;
		var yellow = 0;
		var blue = 0;
		var green = 0;
		for (var i = 0; i < data.length; i++) {
			var value = data[i][yearList[j]];
			var showlevel = value.split('_')[3];
			if (Number(showlevel) == 10) {
				red++;
			}
			else if (Number(showlevel) == 20) {
				orange++;
			}
			else if (Number(showlevel) == 30) {
				yellow++;
			}
			else if (Number(showlevel) == 40) {
				blue++;
			}
			else if (Number(showlevel) == 50) {
				green++;
			}
			years_Data["year"] = yearList[j];
			years_Data["green"] = green;
			years_Data["blue"] = blue;
			years_Data["yellow"] = yellow;
			years_Data["orange"] = orange;
			years_Data["red"] = red;
		}
		color_Data[j] = years_Data;
	}

	jsonXData = [];    //年份数组 放在图里
	series = [];
	var data_red = [];
	var data_orange = [];
	var data_yellow = [];
	var data_blue = [];
	var data_green = [];
	for (var i = 0; i < color_Data.length; i++) {
		jsonXData.push(color_Data[i].year);
		data_green.push(color_Data[i].green);
		data_blue.push(color_Data[i].blue);
		data_yellow.push(color_Data[i].yellow);
		data_orange.push(color_Data[i].orange);
		data_red.push(color_Data[i].red);
	}
	var series_data_green = {"name": "提前达标", "type": "bar", "data": data_green};
	var series_data_blue = {"name": "达到当年标准", "type": "bar", "data": data_blue};
	var series_data_yellow = {"name": "当年未达标经努力预期可达标", "type": "bar", "data": data_yellow};
	var series_data_orange = {"name": "达标困难", "type": "bar", "data": data_orange};
	var series_data_red = {"name": "预期难以达标", "type": "bar", "data": data_red};

	series.push(series_data_green);
	series.push(series_data_blue);
	series.push(series_data_yellow);
	series.push(series_data_orange);
	series.push(series_data_red);
	showData(series, jsonXData);
}
// --------新加统计颜色数量end

//导出Excel文件
function Save_Excel(name) {
	var columsFields=$("#tt").datagrid("getColumnFields");
	var CellStyles=[];

	var arrtable = [];
	var iTr=[];
	var iTable = document.getElementsByTagName("table");
	for (var j = 0; j < iTable.length; j++) {
		if (iTable[j].className == "datagrid-btable") {
			arrtable.push(iTable[j]);
		}
	}
	for (var i = 0; i < arrtable.length; i++) {
		if(arrtable[i].childNodes[0].childNodes.length>0){
			iTr = arrtable[i].getElementsByTagName("tr");
		}
	}

	for(var z=0; z<iTr.length;z++){
		var childNodess=iTr[z].childNodes;
		for (var i = 0; i < childNodess.length; i++) {
			var style={};
			var rgbS=childNodess[i].style.backgroundColor;
			var colorS='';
			if(rgbS==""){
				colorS='#ffffff';
			}else{
				colorS=rgb2hex(rgbS);
			}
			style['BgColor']=colorS;
			style['index']=z;
			style['field']=columsFields[i];
			CellStyles.push(style);
		}
	}

	var grid = $("#tt");
	var rows=grid.datagrid("getRows");					  //获得表格行数据
	var colums_all=grid.datagrid("options").columns;      		     //获得表格的所有列不包括冻结列
	var obj = $.ExportExcelDlg({
		HeadInfo: colums_all,    //表头栏
		RowInfo: rows,		   //数据航
		FooterInfo: grid.datagrid("getFooterRows"),
		RowStart: 2,		   //导出行位置
		ColumStart: 2,		   //导出列位置
		SheetName: name,
		CellStyles:CellStyles,
		MainTitle: { Displayname: name, Alignment: 'Center' },
		SaveName: name,
		Swf: 'ExportExcel.swf'
	});
	obj.ExportExcelDlg('open');
}


function getreportShowColumns() {

	var reportShowColumns = [
		defineTableReportAllColumns["iname"],
		defineTableReportAllColumns["code"],
		defineTableReportAllColumns["unit"],
		defineTableReportAllColumns["sname"]
	];

	var yeares = document.getElementsByName("year");
	var year = "";
	var yearList = [];
	for (var i = 0; i < yeares.length; i++) {
		if (yeares[i].checked) {
			yearList.push(yeares[i].value);
			year += yeares[i].value+",";
		}
	}
	if(year!=""){
		year = year.substring(0, year.length-1);
	}

	for(var i=0; i< yearList.length; i++){
		var c = {title:yearList[i], field:yearList[i], width:90,align:"center",
			sortable:true,
			editor : 'text',
			formatter: function (value, row, index) {
				// 数据上报的类型_每年的目标值_数值_显示数据展示时效果的规则_每年的备注
				var tempValue;
				if(value.indexOf('_')!=-1){
					tempValue = value.split('_')[2];
				}
				if(tempValue===null||tempValue==""||tempValue=="null"||tempValue=="NULL"){
					return "";
				}else{
					return tempValue;
				}
			},
			styler: function (value, row, index) {
				/*
                     10  红色#ff0000  预期难以达标
                     20  橙色#FF9933  达标困难
                     30  黄色#ffff00  当年未达标经努力预期可达标
                     40  浅绿#039dea  达到当年标准
                     50  深绿#009966  提前达标
                 */
				var showlevel = value.split('_')[3];
				switch (Number(showlevel)) {
					case 0 :
						return 'background-color:#FFFFFF;';
					case 10 :
						return 'background-color:#ff0000;';
					case 20 :
						return 'background-color:#FF9933;';
					case 30 :
						return 'background-color:#ffff00;';
					case 40 :
						return 'background-color:#039dea;';
					case 50 :
						return 'background-color:#009966;';
				}
				return 'background-color:#FFFFFF;';
			}
		};
		reportShowColumns.push(c);
	}
	reportShowColumns.push(defineTableReportAllColumns['aim']);//目标规划值
	reportShowColumns.push(defineTableReportAllColumns['rname']);//目标规则
	reportShowColumns.push(defineTableReportAllColumns['idesc']);//目标描述

	return reportShowColumns;

}
